#include <iostream>
#include <stdlib.h>

#include "psUtil.h"
#include "data.h"

using namespace std;

PSUtil::PSUtil() {

}

PSUtil::~PSUtil() {

}

ArgStruct PSUtil::getArgs(int &argc, char** &argv) {
	vector<string> argList;
	for (int i = 0; i < argc; i++){
		argList.push_back(argv[i]);
	}

	ArgStruct args;

 	vector<string>::iterator option;
 	option = find(argList.begin(), argList.end(), "-data");
 	args.dataFileName = (*++option).c_str();

  	option = find(argList.begin(), argList.end(), "-color");
	const char *c = (*++option).c_str();
	args.maxColor = getInt(*c);

 	if(!args.maxColor) {
 		cout << "Error: Color is non-numerical" << endl;
 		exit(1);
 	}

  	option = find(argList.begin(), argList.end(), "-cost");
	if(option != argList.end()) {
		string cost = *++option;

		if (cost.size() != 4) {
			cout << "Error: Wrong cost format" << endl;
			exit (1);
		}

		string::iterator i = cost.begin();

		args.alpha = Data::getInt(*i++);
		args.beta1 = Data::getInt(*i++);
		args.beta2 = Data::getInt(*i++);
		args.gamma = Data::getInt(*i++);

	}

	return args;
}

void PSUtil::setGlobals(int sz, int rk){

}

int PSUtil::getMax(vector<int> grpCount) {
	int max = -9999;
	vector<int>::iterator itr = grpCount.begin();

	while(itr != grpCount.end()) {
		const int x = *itr++;
		if (x > max)
			max = x;
	}

	return max;
}

bool PSUtil::belongsTo(vector<int> V, int ele) {
	vector<int>::iterator itr = find(V.begin(), V.end(), ele);

	if (itr == V.end())
		return false;
	else
		return true;
}

int PSUtil::getInt(char c) {
	return atoi(&c);
}

vector<intVec> PSUtil::transposeMat(vector<intVec> v) {
	int n = v.size();
	int m = v[0].size();

	vector<intVec> trans;

	for (int i = 0; i < m; i++) {
		vector<int> col;
		for (int j = 0; j < n; j++) {
			col.push_back(v[j][i]);
		}
		trans.push_back(col);
	}

	return trans;
}

void PSUtil::printMatrix(intIntVec matrix) {
	for (intIntVec::iterator x = matrix.begin();
	     x != matrix.end(); x++) {
		for (intVec::iterator y = x->begin();
		     y != x->end(); y++) {
			cout << *y << " ";
		}
		cout << endl;
	}
	cout << endl;
}
